<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<#include "../head.html">
		<title>用户管理</title>
		<script type="text/javascript">
		var queryForm,queryDialog,inputForm,inputDialog,grid;
		$(function(){
			queryForm = Rainbow.Form({
				  formId:"#queryForm"
				 ,lable:true
				 ,inputArray:[
					 {lable:"姓名",name:"name",type:"textbox",condition:"like"}
					,{lable:"账号",name:"account.name",type:"textbox",condition:"like"}
				  ]
				 ,queryForm:true
			});
			
			queryDialog = Rainbow.Dialog({
		  		  title:"查询条件"
				 ,dialogId:'#queryDialog'
				 ,form:queryForm
				 ,width: '80%'
			 	 ,height: '50%'
		 		 ,modal: false
			     ,closable:true
			 	 ,buttons:[{
						text:'确认',
						iconCls:'icon-ok',
						handler:function(){
							grid.query();
							queryDialog.dialog('close');
						}
					}]
			});
			
			inputForm = Rainbow.Form({
				  formId:"#inputForm"
				 ,lable:true
				 ,inputArray:[
					 			 {name:"id",type:"hidden"}
					 			,{name:"account.id",type:"hidden"}
					 			,{lable:"姓名",name:"name",option:{required:true}}
					 			,{lable:"账号",name:"account.name",option:{required:true}}
					 			,{lable:"密码",name:"account.password",type:"passwordbox"}
					 			,{lable:"角色",name:"roles",type:"combogrid",option:Rainbow.inputOption.combogrid({
					 				 serviceName:"RoleService"
				 					,multiple: true
					 				,columns: 
										[[
					  						 {field:'id',title:'选择',checkbox:true}
					  						,{field:'name',title:'名称',width:150}
					 				    ]]
					 				})
					 			 }
							]
			 	 ,colNum:1
			});
			
			inputDialog = Rainbow.Dialog({
				  title:"用户"
				 ,width:400
				 ,height:300
				 ,dialogId:'#inputDialog'
				 ,form:inputForm
				 ,serviceName:"UserService"
				 ,methodName:"save"
				 ,callback:function(result){
					 grid.query();
				 }
			 	,beforeSendData:function(data){
			 		if(data.roles){
			 			var userRoles = [];
			 			for(var i=0;i<data.roles.length;i++){
			 				userRoles.push({role:{id:data.roles[i]}})
			 			}
				 		data["userRoles"] = userRoles;
			 		}
			 	}
			 });
			
			grid = Rainbow.Grid({
				  serviceName:"UserService"
				 ,queryForm:queryForm
				 ,paths:["account"]
				 ,columns: 
						[[
							 {field:'id',title:'选择',checkbox:true}
							,{field:'name',title:'姓名',width:150}
							,{field:'account.name',title:'账号',width:150
								,formatter: function(value,row,index){
									if(row.account){
										return row.account.name
									}
					        		
								}
							}
					    ]]
				,onDblClickRow: function (index, row) {
					grid.datagrid('selectRow',index);
				 	grid.update(inputDialog,inputForm);
		     	}
				,onUpdate:function(data){
					var roles = [];
					if(data.userRoles){
						for(var i=0;i<data.userRoles.length;i++){
							roles.push(data.userRoles[i].role.id);
						}
						data["roles"] = roles;
					}
				}
			});
		});
		</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
		<div id="toolbar"></div>
		<div id="grid"></div>
		<div id="queryDialog">
			<div id="queryForm"></div>
		</div>
		<div id="inputDialog">
			<form id="inputForm"></form>
		</div>
	</body>
	<script type="text/javascript">
		Rainbow.Util.initButton("#toolbar");
	</script>
</html>